
/**
 * @Project : Clion_ProgrammingExercises
 * @File : 0240
 * @Author : WangRuoyu 
 * @Date : 2023/9/20 16:18
 */

#include "iostream"

using namespace std;

const int N = 1e5 + 10;
int f[N], d[N];

int find(int x) {
    if (f[x] != x) {
        int t = find(f[x]);
        d[x] += d[f[x]];
        f[x] = t;
    }
    return f[x];
}

int main() {
    int n, m;
    scanf("%d %d", &n, &m);

    for (int i = 1; i <= n; ++i) {
        f[i] = i;
    }

    int res = 0;

    for (int i = 0; i < m; ++i) {
        int t, x, y;
        scanf("%d %d %d", &t, &x, &y);

        if (x > n || y > n) {
            res++;
        } else {
            int fx = find(x);
            int fy = find(y);
            if (t == 1) {
                if (fx == fy && (d[x] - d[y]) % 3) {
                    res++;
                } else if (fx != fy) {
                    f[fx] = fy;
                    d[fx] = d[y] - d[x];
                }
            } else {
                if (fx == fy && (d[x] - d[y] - 1) % 3) {
                    res++;
                } else if (fx != fy) {
                    f[fx] = fy;
                    d[fx] = d[y] + 1 - d[x];
                }
            }
        }
    }

    printf("%d", res);

    return 0;
}